package com.ruoyi.price.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.price.domain.TbFormula;
import com.ruoyi.price.domain.TbFormulaWidget;
import com.ruoyi.price.domain.TbFormulaWidgetAttr;
import com.ruoyi.price.domain.dto.TbFormulaDTO;
import com.ruoyi.price.domain.dto.TbFormulaWidgetDTO;
import com.ruoyi.price.mapper.TbFormulaMapper;
import com.ruoyi.price.service.ITbFormulaService;
import com.ruoyi.price.service.ITbFormulaWidgetAttrService;
import com.ruoyi.price.service.ITbFormulaWidgetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 公式信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class TbFormulaServiceImpl extends ServiceImpl<TbFormulaMapper, TbFormula> implements ITbFormulaService
{
    @Autowired
    private ITbFormulaWidgetService widgetService;
    @Autowired
    private ITbFormulaWidgetAttrService widgetAttrService;

    @Override
    public List<TbFormula> listByCondition(TbFormula tbFormula, SysUser sysUser) {
        return list(new QueryWrapper<TbFormula>().lambda()
                .eq(StringUtils.isNotEmpty(tbFormula.getName()),TbFormula::getName,tbFormula.getName())
                .eq(TbFormula::getStatus,0));
    }

    @Override
    public boolean saveFormula(TbFormulaDTO formulaDTO) {
        //1.保存公式基础信息
        TbFormula formula = new TbFormula();
        BeanUtils.copyProperties(formulaDTO,formula);
        formula.setCode(IdWorker.getIdStr());
        formula.setStatus("0");
        final boolean[] result = {save(formula)};
        Long formulaId = formula.getId();
        //2.保存控件信息
        List<TbFormulaWidgetDTO> formulaWidgetDTOList = formulaDTO.getFormulaWidgetList();
        formulaWidgetDTOList.forEach(item->{
            TbFormulaWidget formulaWidget = new TbFormulaWidget();
            BeanUtils.copyProperties(item,formulaWidget);
            formulaWidget.setFormualId(formulaId);
            formulaWidget.setStatus("0");
            formulaWidget.setCode(IdWorker.getIdStr());

            result[0] = widgetService.save(formulaWidget);

            //3.保存控件属性信息
            List<TbFormulaWidgetAttr> formulaWidgetAttrList = item.getFormulaWidgetAttrList();
            formulaWidgetAttrList.forEach(formulaWidgetAttr->{
                formulaWidgetAttr.setFormualId(formulaId);
                formulaWidgetAttr.setWidgetId(formulaWidget.getId());
                formulaWidgetAttr.setStatus("0");
            });
            result[0] = widgetAttrService.saveBatch(formulaWidgetAttrList);
        });
        return result[0];
    }

    @Override
    public TbFormulaDTO getFormulaById(Long id) {
        TbFormulaDTO formulaDTO = new TbFormulaDTO();
        // 查询公式信息
        TbFormula formula = getById(id);

        BeanUtils.copyProperties(formula,formulaDTO);

        // 查询控件以及属性信息
        List<TbFormulaWidgetDTO> formulaWidgetList = widgetService.listByFormulaId(id);
        formulaDTO.setFormulaWidgetList(formulaWidgetList);
        return formulaDTO;
    }

    @Override
    public boolean updateFormulaById(TbFormulaDTO formulaDTO) {
        //1.保存公式基础信息
        TbFormula formula = new TbFormula();
        BeanUtils.copyProperties(formulaDTO,formula);
        final boolean[] result = {updateById(formula)};
        Long formulaId = formula.getId();
        //2.保存控件信息
        List<TbFormulaWidgetDTO> formulaWidgetDTOList = formulaDTO.getFormulaWidgetList();
        // 删除原来的控件以及控件属性
        widgetService.removeByFormulaId(formulaId);
        widgetAttrService.removeByFormulaId(formulaId);
        // 保存最新的控件以及控件属性
        formulaWidgetDTOList.forEach(item->{
            TbFormulaWidget formulaWidget = new TbFormulaWidget();
            BeanUtils.copyProperties(item,formulaWidget);
            formulaWidget.setFormualId(formulaId);
            formulaWidget.setStatus("0");
            formulaWidget.setCode(IdWorker.getIdStr());

            result[0] = widgetService.save(formulaWidget);

            //3.保存控件属性信息
            List<TbFormulaWidgetAttr> formulaWidgetAttrList = item.getFormulaWidgetAttrList();
            formulaWidgetAttrList.forEach(formulaWidgetAttr->{
                formulaWidgetAttr.setFormualId(formulaId);
                formulaWidgetAttr.setWidgetId(formulaWidget.getId());
                formulaWidgetAttr.setStatus("0");
            });
            result[0] = widgetAttrService.saveBatch(formulaWidgetAttrList);
        });

        return result[0];
    }

    @Override
    public boolean removeFormulaByIds(List<String> idList) {
        // 1.删除公式
        boolean result = removeByIds(idList);
        // 2.删除控件
        result = widgetService.removeByFormulaIds(idList);
        // 3.删除控件属性
        result = widgetAttrService.removeByFormulaIds(idList);
        return result;
    }

    @Override
    public List<TbFormula> getFormulaData() {
        return list(new QueryWrapper<TbFormula>().lambda());
    }
}
